class User < ActiveRecord::Base

  before_create :encrypt_password

  attr_accessor :password, :password_confirmation

  validates_presence_of :username
  validates_presence_of :password
  validates_presence_of :email
  validates_presence_of :first_name
  validates_presence_of :last_name
  
  validates_confirmation_of :password_confirmation

  def authenticate
    return true if self.crypted_password == generate_hash(self.password) 
  end

  private
  def generate_hash(value)
    Digest::SHA1.hexdigest(value)
  end
  
  def encrypt_password
    self.crypted_password = generate_hash(self.password)
  end

end
